package com.gehj.androidlibs.Utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Xiang on 2018/10/26.
 */
public class ReadFileUtil {

	public static String ReadTxtFile(File file)
	{
		String content = "";
		if (!file.isDirectory()) {  //检查此路径名的文件是否是一个目录(文件夹)
			if (file.getName().endsWith("txt")) {//文件格式为""文件
				try {
					InputStream instream = new FileInputStream(file);
					if (instream != null) {
						InputStreamReader inputreader
								= new InputStreamReader(instream, "UTF-8");
						BufferedReader buffreader = new BufferedReader(inputreader);
						String line = "";
						//分行读取
						while ((line = buffreader.readLine()) != null) {
							content += line + "\n";
						}
						instream.close();//关闭输入流
					}
				} catch (FileNotFoundException e) {
					Log.d("TestFile", "The File doesn't not exist.");
				} catch (IOException e) {
					Log.d("TestFile", e.getMessage());
				}
			}
		}
		return content;

	}

	public static String ReadTxtFile_ip(File file){

		StringBuffer stringBuffer = new StringBuffer("");
		if (!file.isDirectory()) {  //检查此路径名的文件是否是一个目录(文件夹)
				if (file.getName().endsWith("txt")) {//文件格式为""文件
					try {
						FileReader fileReader = new FileReader(file);
						int ch = 0;
						while((ch = fileReader.read())!=-1 ){
							stringBuffer.append((char)ch);
						}
						fileReader.close();
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}


		return stringBuffer.toString();
	}

}
